<!--
  * 公司实名认证记录表
  *
  * @Author:    zzb
  * @Date:      2025-01-23 04:08:26
-->
<template>
  <a-modal :title="form.id ? '编辑' : '添加'" :width="800" :open="visibleFlag" @cancel="onClose" :maskClosable="false" :destroyOnClose="true">
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }">
      <a-form-item label="id" name="id">
        <a-input style="width: 100%" v-model:value="form.id" placeholder="id" />
      </a-form-item>
      <a-form-item label="企业类型 0企业 1" name="coType">
        <a-input-number style="width: 100%" v-model:value="form.coType" placeholder="企业类型 0企业 1" />
      </a-form-item>
      <a-form-item label="证件类型 0营业执照 1" name="certType">
        <a-input-number style="width: 100%" v-model:value="form.certType" placeholder="证件类型 0营业执照 1" />
      </a-form-item>
      <a-form-item label="统一社会信用代码" name="creditCode">
        <a-input style="width: 100%" v-model:value="form.creditCode" placeholder="统一社会信用代码" />
      </a-form-item>
      <a-form-item label="注册地城市" name="regCity">
        <DictSelect width="100%" v-model:value="form.regCity" keyCode="" placeholder="注册地城市" />
      </a-form-item>
      <a-form-item label="企业注册地址" name="regAddress">
        <DictSelect width="100%" v-model:value="form.regAddress" keyCode="" placeholder="企业注册地址" />
      </a-form-item>
      <a-form-item label="经营范围" name="businessScope">
        <a-textarea style="width: 100%" v-model:value="form.businessScope" placeholder="经营范围" />
      </a-form-item>
      <a-form-item label="营业期限" name="businessTerm">
        <a-textarea style="width: 100%" v-model:value="form.businessTerm" placeholder="营业期限" />
      </a-form-item>
      <a-form-item label="单位联系电话" name="contactPhone">
        <a-input-number style="width: 100%" v-model:value="form.contactPhone" placeholder="单位联系电话" />
      </a-form-item>
      <a-form-item label="开户行编号" name="bankCode">
        <DictSelect width="100%" v-model:value="form.bankCode" keyCode="" placeholder="开户行编号" />
      </a-form-item>
      <a-form-item label="联行号" name="bankBranchCode">
        <a-input style="width: 100%" v-model:value="form.bankBranchCode" placeholder="联行号" />
      </a-form-item>
      <a-form-item label="结算卡号" name="debitCardNo">
        <a-input style="width: 100%" v-model:value="form.debitCardNo" placeholder="结算卡号" />
      </a-form-item>
      <a-form-item label="选择证件类型" name="idType">
        <DictSelect width="100%" v-model:value="form.idType" keyCode="" placeholder="选择证件类型" />
      </a-form-item>
      <a-form-item label="开户人姓名" name="accountHolderName">
        <a-input style="width: 100%" v-model:value="form.accountHolderName" placeholder="开户人姓名" />
      </a-form-item>
      <a-form-item label="授权人身份证号码" name="legalPersonId">
        <a-input-number style="width: 100%" v-model:value="form.legalPersonId" placeholder="授权人身份证号码" />
      </a-form-item>
      <a-form-item label="身份证有效日期" name="idCardDate">
        <DictSelect width="100%" v-model:value="form.idCardDate" keyCode="" placeholder="身份证有效日期" />
      </a-form-item>
      <a-form-item label="授权人手机号" name="authPhone">
        <a-input-number style="width: 100%" v-model:value="form.authPhone" placeholder="授权人手机号" />
      </a-form-item>
      <a-form-item label="营业执照图片URL" name="license">
        <FileUpload
          :defaultFileList="form.license"
          :folder="FILE_FOLDER_TYPE_ENUM.COMMON.value"
          buttonText="上传 营业执照图片URL"
          listType="text"
          @change="(e) => (form.license = e)"
        />
      </a-form-item>
      <a-form-item label="身份证正面图片URL" name="idCardFront">
        <FileUpload
          :defaultFileList="form.idCardFront"
          :folder="FILE_FOLDER_TYPE_ENUM.COMMON.value"
          buttonText="上传 身份证正面图片URL"
          listType="text"
          @change="(e) => (form.idCardFront = e)"
        />
      </a-form-item>
      <a-form-item label="身份证背面图片URL" name="idCardBack">
        <FileUpload
          :defaultFileList="form.idCardBack"
          :folder="FILE_FOLDER_TYPE_ENUM.COMMON.value"
          buttonText="上传 身份证背面图片URL"
          listType="text"
          @change="(e) => (form.idCardBack = e)"
        />
      </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue'
  import _ from 'lodash'
  import { message } from 'ant-design-vue'
  import { SmartLoading } from '/@/components/framework/smart-loading'
  import { certRecordApi } from '/src/api/business/b-agent/enterprise/cert-record-api'
  import { smartSentry } from '/@/lib/smart-sentry'
  import FileUpload from '/@/components/support/file-upload/index.vue'
  import DictSelect from '/@/components/business/comment/DataDictionariesSelect.vue'
  import { FILE_FOLDER_TYPE_ENUM } from '/@/constants/support/file-const'

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList'])

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false)

  function show(rowData) {
    Object.assign(form, formDefault)
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData)
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true
    nextTick(() => {
      formRef.value.clearValidate()
    })
  }

  function onClose() {
    Object.assign(form, formDefault)
    visibleFlag.value = false
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref()

  const formDefault = {
    id: undefined, //id
    coType: undefined, //企业类型 0企业 1
    certType: undefined, //证件类型 0营业执照 1
    creditCode: undefined, //统一社会信用代码
    regCity: undefined, //注册地城市
    regAddress: undefined, //企业注册地址
    businessScope: undefined, //经营范围
    businessTerm: undefined, //营业期限
    contactPhone: undefined, //单位联系电话
    bankCode: undefined, //开户行编号
    bankBranchCode: undefined, //联行号
    debitCardNo: undefined, //结算卡号
    idType: undefined, //选择证件类型
    accountHolderName: undefined, //开户人姓名
    legalPersonId: undefined, //授权人身份证号码
    idCardDate: undefined, //身份证有效日期
    authPhone: undefined, //授权人手机号
    license: undefined, //营业执照图片URL
    idCardFront: undefined, //身份证正面图片URL
    idCardBack: undefined, //身份证背面图片URL
  }

  let form = reactive({ ...formDefault })

  const rules = {
    id: [{ required: true, message: 'id 必填' }],
    coType: [{ required: true, message: '企业类型 0企业 1 必填' }],
    certType: [{ required: true, message: '证件类型 0营业执照 1 必填' }],
    creditCode: [{ required: true, message: '统一社会信用代码 必填' }],
    regCity: [{ required: true, message: '注册地城市 必填' }],
    regAddress: [{ required: true, message: '企业注册地址 必填' }],
    businessScope: [{ required: true, message: '经营范围 必填' }],
    businessTerm: [{ required: true, message: '营业期限 必填' }],
    contactPhone: [{ required: true, message: '单位联系电话 必填' }],
    bankCode: [{ required: true, message: '开户行编号 必填' }],
    bankBranchCode: [{ required: true, message: '联行号 必填' }],
    debitCardNo: [{ required: true, message: '结算卡号 必填' }],
    idType: [{ required: true, message: '选择证件类型 必填' }],
    accountHolderName: [{ required: true, message: '开户人姓名 必填' }],
    legalPersonId: [{ required: true, message: '授权人身份证号码 必填' }],
    idCardDate: [{ required: true, message: '身份证有效日期 必填' }],
    authPhone: [{ required: true, message: '授权人手机号 必填' }],
    license: [{ required: true, message: '营业执照图片URL 必填' }],
    idCardFront: [{ required: true, message: '身份证正面图片URL 必填' }],
    idCardBack: [{ required: true, message: '身份证背面图片URL 必填' }],
  }

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields()
      save()
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!')
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show()
    try {
      if (form.id) {
        await certRecordApi.update(form)
      } else {
        await certRecordApi.add(form)
      }
      message.success('操作成功')
      emits('reloadList')
      onClose()
    } catch (err) {
      smartSentry.captureError(err)
    } finally {
      SmartLoading.hide()
    }
  }

  defineExpose({
    show,
  })
</script>
